package com.tanhua.admin.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.CircleCaptcha;
import com.tanhua.admin.service.AdminService;
import com.tanhua.commons.utils.Constants;
import com.tanhua.vo.AdminVo;
import com.tanhua.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/system/users")
public class SystemController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    /**
     * 生成验证码图片
     * @param uuid
     * @param response
     * @return
     */
    @GetMapping("/verification")
    private void verification(String uuid,HttpServletResponse response) throws IOException {
        //生成验证码对象
        CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(299, 97);
        //将验证码存入redis
        String key = Constants.CAP_CODE+uuid;
        String code = captcha.getCode();
        redisTemplate.opsForValue().set(key,code);
        //将验证码写入response流
        captcha.write(response.getOutputStream());
    }

    /**
     * 用户登录逻辑
     * @param map
     * @return
     */
    @PostMapping("/login")
    private ResponseEntity login(@RequestBody Map map){
       Map result = adminService.login(map);
       return ResponseEntity.ok(result);
    }

    /**
     * 获取用户信息
     * @return
     */
    @PostMapping("/profile")
    public ResponseEntity profile() {
        AdminVo vo = adminService.profile();
        return ResponseEntity.ok(vo);
    }

}
